﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Web.Mvc;
using Model.WaiGaoQiao;
using WaiGaoQiaoWeb.Models;
using WaiGaoQiaoWeb.JsonEntities;

namespace WaiGaoQiaoWeb.Controllers
{
    public class OneCanteenSummaryReportController : Controller
    {
        private OneCanteenSummaryReportInfo fundinfo = new OneCanteenSummaryReportInfo();
        public ActionResult OneCanteenSummaryReportIndex()
        {
            ViewData["Deptment"] = GetDeptment();
            OneCanteenSummaryReportVM vm = new OneCanteenSummaryReportVM();
            vm.chALL = true;
            vm.chBF = true;
            vm.chDX = true;
            vm.chJBF = true;
            vm.chLKXF = true;
            vm.chQD = true;
            vm.chSG = true;
            vm.chWL = true;
            vm.chWM = true;
            vm.chZDJ = true;
            return View(vm);
        }

        /// <summary>
        /// 获取部门列表（用于绑定下拉框）
        /// </summary>
        /// <returns></returns>
        public IList<SelectListItem> GetDeptment()
        {
            IDictionary<int, string> dic = new Dictionary<int, string>();
            DataSet ds = fundinfo.GetdeptName();
            dic.Add(-1, "全部");
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                dic.Add(Convert.ToInt32(ds.Tables[0].Rows[i]["Dep_ID"]), ds.Tables[0].Rows[i]["deptName"].ToString().Trim());
            }

            IList<SelectListItem> selectList =
                dic.Select(m => new SelectListItem() { Text = m.Value.ToString(), Value = m.Key.ToString() }).ToList();
            return selectList;
        }

        public ActionResult GetList(PageVM pvm)
        {
            OneCanteenSummaryReportInfo info = new OneCanteenSummaryReportInfo();
            string condition = pvm.query;
            info.StartTime = Convert.ToDateTime(condition.Split('&')[0]);
            info.EndTime = Convert.ToDateTime(condition.Split('&')[1]);
            info.DEP_ID = "%";
            info.EMPNO = "%";
            info.PNAME = "%";
            info.MacNo = "%";
            if (condition.Split('&')[2].ToString() != "-1")
            {
                info.DEP_ID = condition.Split('&')[2].ToString();
            }
            if (condition.Split('&')[3].ToString() != "")
            {
                info.EMPNO = condition.Split('&')[3].ToString();
            }
            if (condition.Split('&')[4].ToString() != "")
            {
                info.PNAME = condition.Split('&')[4].ToString();
            }
            if (condition.Split('&')[5].ToString() != "")
            {
                info.MacNo = condition.Split('&')[5].ToString();
            }
            if (condition.Split('&')[6].ToString() != "")
            {
                string strall = condition.Split('&')[6].ToString();
                info.All = strall.Remove(strall.Length - 1);
            }
            JsonFlexiGridData jsonFlexiGridData = new JsonFlexiGridData();
            jsonFlexiGridData.page = pvm.page;
            string tablename = "TMP1HZ_" + DateTime.Now.ToString("yyyyMMddHHmmss");
            int count;
            DataSet list = fundinfo.BindList(pvm.rp, pvm.page, info, tablename, out count);
            jsonFlexiGridData.total = count;
            List<string> v;
            foreach (DataRow item in list.Tables[0].Rows)
            {
                #region
                v = new List<string>();
                v.Add(item["SN"].ToString());
                v.Add(item["SDAY"].ToString());
                v.Add(item["COUNTM"].ToString());
                v.Add(item["SUMM"].ToString());
                v.Add(item["COUNTM1"].ToString());
                v.Add(item["SUMM1"].ToString());
                v.Add(item["COUNTM2"].ToString());
                v.Add(item["SUMM2"].ToString());
                v.Add(item["COUNTM3"].ToString());
                v.Add(item["SUMM3"].ToString());
                v.Add(item["COUNTM4"].ToString());
                v.Add(item["SUMM4"].ToString());
                #endregion
                FlexiGridRow fgr = new FlexiGridRow()
                {
                    id = item["SN"].ToString(),
                    cell = v
                };
                jsonFlexiGridData.rows.Add(fgr);
            }
            return Json(jsonFlexiGridData);
        }

        /// <summary>
        /// 一食堂汇总报表导出
        /// </summary>
        /// <returns></returns>
        public ActionResult ReportOut(string StartTime, string EndTime, string DEP_ID, string EMPNO, string PNAME, string MacNo, string ALL)
        {
            return EContent(StartTime, EndTime, DEP_ID, EMPNO, PNAME, MacNo, ALL);
        }

        public ExcelContentResult EContent(string startTime, string endTime, string depid, string empno, string pname, string macno, string all)
        {
            OneCanteenSummaryReportInfo info = new OneCanteenSummaryReportInfo();
            info.StartTime = Convert.ToDateTime(startTime);
            info.EndTime = Convert.ToDateTime(endTime);
            info.DEP_ID = "%";
            info.EMPNO = "%";
            info.PNAME = "%";
            info.MacNo = "%";
            if (depid != "-1")
            {
                info.DEP_ID = depid;
            }
            if (empno != "")
            {
                info.EMPNO = empno;
            }
            if (pname != "")
            {
                info.PNAME = pname;
            }
            if (macno != "")
            {
                info.MacNo = macno;
            }
            if (all != "")
            {
                info.All = all.Remove(all.Length - 1);
            }
            string tablename = "TMP1HZ_" + DateTime.Now.ToString("yyyyMMddHHmmss");
            StringBuilder sBuilder = new StringBuilder();
            DataSet list = fundinfo.GetListAll(info, tablename);
            sBuilder.Append("<table cellspacing='0' cellpadding='5' rules='all' border='1'>");
            sBuilder.Append("<tr style='white-space: nowrap;'>");
            sBuilder.Append("<td colspan='11' style='font-size:25px; text-align:center;'>一食堂消费汇总报表</td>");
            sBuilder.Append("</tr>");
            sBuilder.Append("<tr style='font-size:17px;white-space: nowrap;'>");
            sBuilder.Append("<td rowspan='2'>日期</td>");
            sBuilder.Append("<td colspan='2'>合计</td>");
            sBuilder.Append("<td colspan='2'>早餐</td>");
            sBuilder.Append("<td colspan='2'>中餐</td>");
            sBuilder.Append("<td colspan='2'>晚餐</td>");
            sBuilder.Append("<td colspan='2'>夜餐</td>");
            sBuilder.Append("</tr>");
            sBuilder.Append("<tr style='font-size:17px;white-space: nowrap;'>");
            sBuilder.Append("<td>次数</td>");
            sBuilder.Append("<td>金额</td>");
            sBuilder.Append("<td>次数</td>");
            sBuilder.Append("<td>金额</td>");
            sBuilder.Append("<td>次数</td>");
            sBuilder.Append("<td>金额</td>");
            sBuilder.Append("<td>次数</td>");
            sBuilder.Append("<td>金额</td>");
            sBuilder.Append("<td>次数</td>");
            sBuilder.Append("<td>金额</td>");
            sBuilder.Append("</tr>");
            int COUNTMAll = 0;
            int COUNTM1All = 0;
            int COUNTM2All = 0;
            int COUNTM3All = 0;
            int COUNTM4All = 0;
            decimal SUMMAll = 0.0M;
            decimal SUMM1All = 0.0M;
            decimal SUMM2All = 0.0M;
            decimal SUMM3All = 0.0M;
            decimal SUMM4All = 0.0M;
            foreach (DataRow item in list.Tables[0].Rows)
            {
                sBuilder.Append("<tr style='font-size:14px; white-space: nowrap;'>");
                sBuilder.Append("<td>" + item["SDAY"].ToString() + "</td>");
                sBuilder.Append("<td>" + item["COUNTM"].ToString() + "</td>");
                sBuilder.Append("<td>" + Math.Round(Convert.ToDecimal(item["SUMM"]), 1, MidpointRounding.AwayFromZero) + "</td>");
                sBuilder.Append("<td>" + item["COUNTM1"].ToString() + "</td>");
                sBuilder.Append("<td>" + Math.Round(Convert.ToDecimal(item["SUMM1"]), 1, MidpointRounding.AwayFromZero) + "</td>");
                sBuilder.Append("<td>" + item["COUNTM2"].ToString() + "</td>");
                sBuilder.Append("<td>" + Math.Round(Convert.ToDecimal(item["SUMM2"]), 1, MidpointRounding.AwayFromZero) + "</td>");
                sBuilder.Append("<td>" + item["COUNTM3"].ToString() + "</td>");
                sBuilder.Append("<td>" + Math.Round(Convert.ToDecimal(item["SUMM3"]), 1, MidpointRounding.AwayFromZero) + "</td>");
                sBuilder.Append("<td>" + item["COUNTM4"].ToString() + "</td>");
                sBuilder.Append("<td>" + Math.Round(Convert.ToDecimal(item["SUMM4"]), 1, MidpointRounding.AwayFromZero) + "</td>");
                sBuilder.Append("</tr>");
                COUNTMAll += Convert.ToInt32(item["COUNTM"]);
                COUNTM1All += Convert.ToInt32(item["COUNTM1"]);
                COUNTM2All += Convert.ToInt32(item["COUNTM2"]);
                COUNTM3All += Convert.ToInt32(item["COUNTM3"]);
                COUNTM4All += Convert.ToInt32(item["COUNTM4"]);
                SUMMAll += Convert.ToDecimal(item["SUMM"]);
                SUMM1All += Convert.ToDecimal(item["SUMM1"]);
                SUMM2All += Convert.ToDecimal(item["SUMM2"]);
                SUMM3All += Convert.ToDecimal(item["SUMM3"]);
                SUMM4All += Convert.ToDecimal(item["SUMM4"]);
            }
            sBuilder.Append("<tr style='font-size:14px; white-space: nowrap;'>");
            sBuilder.Append("<td>合计：</td>");
            sBuilder.Append("<td>" + COUNTMAll + "</td>");
            sBuilder.Append("<td>" + Math.Round(Convert.ToDecimal(SUMMAll), 1, MidpointRounding.AwayFromZero) + "</td>");
            sBuilder.Append("<td>" + COUNTM1All + "</td>");
            sBuilder.Append("<td>" + Math.Round(Convert.ToDecimal(SUMM1All), 1, MidpointRounding.AwayFromZero) + "</td>");
            sBuilder.Append("<td>" + COUNTM2All + "</td>");
            sBuilder.Append("<td>" + Math.Round(Convert.ToDecimal(SUMM2All), 1, MidpointRounding.AwayFromZero) + "</td>");
            sBuilder.Append("<td>" + COUNTM3All + "</td>");
            sBuilder.Append("<td>" + Math.Round(Convert.ToDecimal(SUMM3All), 1, MidpointRounding.AwayFromZero) + "</td>");
            sBuilder.Append("<td>" + COUNTM4All + "</td>");
            sBuilder.Append("<td>" + Math.Round(Convert.ToDecimal(SUMM4All), 1, MidpointRounding.AwayFromZero) + "</td>");
            sBuilder.Append("</tr>");
            sBuilder.Append("</table>");
            ExcelContentResult econtent = new ExcelContentResult();
            econtent.Content = sBuilder.ToString();
            econtent.filename = "yishitanghuizong";
            econtent.ContentType = "application/ms-excel";
            econtent.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8");
            return econtent;
        }
    }
}
